<?php
/**
 * 表单字段自定义管理
 * jzzmly
 */
class EAdmin_Page_FormManage extends EAdmin_Page_Abstract
{
	private $resumeDb;
	private function loadDb()
	{
		$this->resumeDb = Db_Resume::instance();
	}
	public function validate(ME_Request $input, ME_Response $output)
	{
		$euserInfo = Libs_EUser::getLoginStatus($input,$output);
		$output->euid = 
			$input->euid =
			$euid = $euserInfo[0];

		if(empty($euid)){
			header("location:index.php?c=login");
		}

		$state = ME_Config::get("ResumeState");
		$output->state = $state['state'];
		$output->eduDegree = $eduDegree = $state['eduDegree'];

		$dataArr = Libs_Admin_Category::getCategoryList(array('module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		$newDataArr = array();
		if($dataArr){
			foreach($dataArr as $data){
				$id = !empty($data['id']) ? $data['id'] : 0;
				if($id == 0){
					continue;
				}
				$newDataArr[$id] = $data['name'];
			}
		}
		$output->cityArr = $newDataArr;


		$euid && $stateinfo = Libs_EState::getEResumeState($euid);
		if(!empty($stateinfo)){
			$state_arr = explode(",",$stateinfo);
		}else{
			$state_arr = array();
		}

		$output->state_arr = $state_arr;


		return true;
	}

	public function doDefault(ME_Request $input, ME_Response $output)
	{
		die('错误的请求');
		$output->setTemplate("ResumeManage");
	}
	
	
	//get list
	public function doList(ME_Request $input, ME_Response $output)
	{
		$condition = " and firm_id='{$output->euid}'";
		$db = Db_Resume::instance();
		
		//delete
		if($input->post('delBtn')){
			$idArr = $input->post('id');
			if($idArr){
				$more .= ' and id in('.implode(',',$idArr).')';
				$db->query("Delete From `e_form_field` Where 1 {$condition} {$more}");
			}
		}

		//列表数据
		$sql = "Select * From `e_form_field` Where 1 $condition";
		$output->dataList =
			$dataList = $db->getAll($sql);

		$output->setTemplate("FormList");
	}
	
	//add 
	public function doAdd(ME_Request $input, ME_Response $output)
	{
		//post
		if($input->post('addSubmit')){
			$addParam = '';
			$now = time();
			$addParam['firm_id'] = $output->euid;
			$addParam['name'] = trim($input->post('name'));
			$addParam['default'] = trim($input->post('default'));
			
			//add
			$insertId = Libs_EDbHand::addFormField(array('arr'=>$addParam));
		    if($insertId){
		    	self::showMsg('添加成功','/index.php?c=FormManage&a=List');
		    } else {
		    	self::showMsg('网络连接失败，请稍候再试','index.php?c=FormManage&a=Add');
		    }
		    die();
		}

		$output->setTemplate("FormAdd");
	}
	
	
	//edit
	public function doEdit(ME_Request $input, ME_Response $output)
	{
		$id = $input->request('id'); //编辑的id
		if(!$id) die('您要请求的内容不存在');

		$condition = " and firm_id='{$output->euid}' and id='{$id}'";
		$db = Db_Resume::instance();
		$sql = "Select * From `e_form_field` Where 1 {$condition} ";
		$dataInfo = $db->getRow($sql);
		if(!$dataInfo) die('您要请求的内容不存在');

		//post
		if($input->post('updateSubmit')){
			$addParam = '';
			$addParam['name'] = trim($input->post('name'));
			$addParam['default'] = trim($input->post('default'));

			//update
			$res = Libs_EDbHand::updateFormField(array('arr'=>$addParam,'where'=>" where id='{$id}' "));
			if($res){
				self::showMsg('更新成功','/index.php?c=FormManage&a=List');
			} else {
				self::showMsg('网络连接失败，请稍候再试','index.php?c=FormManage&a=Edit&id='.$id);
			}
			die();
		}

		//赋值
		$output->dataInfo = $dataInfo;

		$output->setTemplate("FormEdit");
	}
	
	
	

}
